const {Router} = require('express')
const router = new Router()
const db = require("./db.js");
const uitl = require("./uitl.js");

//下面是一些供前端使用的测试接口；
//get和post方法，对从前端接收的请求data的处理会有区别，可以注意一下；
// app.get("/", (req, res) => res.send("Hello World!"));

// 登录
router.post("/login", (req, res, next) => {
    let body = req.body;
    // console.log("login1", body);
    let sqlParam = {
        user: body.user,
        password: body.password,
        login_time: uitl.getNewTime()
    };
    // console.log("login2", sqlParam);
    db.loginGet({user: sqlParam.user},(res1)=>{
        let obj = res1[0];
        if(obj && obj.password === body.password){
            res.status(200).json({
                status:200,
                data: {
                    ...obj,
                    password: '',
                    token: new Date().getTime() + 3600000 // token有效期1小时
                },
                msg:'登录成功'
            });
        }else {
            res.status(200).json({
                status:401,
                msg:'账号或密码错误'
            });
        }
    });
});
// 学员-新增
router.post("/user/add", (req, res, next) => {
    let body = req.body;
    // console.log("dbAdd", req.body);
    let sqlParam = {
        name: body.name,
        tell: body.tell,
        qq: body.qq,
        type: body.type,
        address: body.address,
        remark: body.remark,
        create_time: uitl.getNewTime()
    };
    // console.log("dbAdd", sqlParam);
    db.dbAdd("cont_user", sqlParam, res, next);
});
// 学员-删除
router.post("/user/delete", (req, res, next) => {
    let body = req.body;
    // console.log("delet", req.body);
    let sqlParam = {
        id: body.id,
    };
    // console.log("delet", sqlParam);
    db.dbDelete("cont_user", sqlParam, res, next);
});
// 学员-编辑
router.post("/user/update", (req, res, next) => {
    let body = req.body;
    // console.log("update", req.body);
    let sqlParam = {
        name: body.name,
        tell: body.tell,
        qq: body.qq,
        type: body.type,
        address: body.address,
        remark: body.remark,
        edit_time: uitl.getNewTime()
    };
    // console.log("update", sqlParam);
    db.dbUpdate("cont_user", sqlParam, res, next, body.id);
});
// 学员-通过id查询
router.post("/user/queryId", (req, res, next) => {
    let body = req.body;
    // console.log("queryById_data", req.body);
    let sqlParam = {
        id: body.id,
    };
    // console.log("queryById_data", sqlParam);
    db.dbQueryById("cont_user", sqlParam, res, next);
});
// 学员-查询列表
router.get("/user/query", (req, res, next) => {
    db.queryList("cont_user", req, res, next);
});

// 证件编号-新增
router.post("/num/add", (req, res, next) => {
    let body = req.body;
    // console.log("dbAdd", req.body);
    let sqlParam = {
        name: body.name,
        file_number: body.file_number,
        create_time: uitl.getNewTime()
    };
    // console.log("dbAdd", sqlParam);
    db.dbAdd("file_num", sqlParam, res, next);
});
// 档案-删除
router.post("/num/delete", (req, res, next) => {
    let body = req.body;
    // console.log("delet", req.body);
    let sqlParam = {
        bid: body.bid,
    };
    // console.log("delet", sqlParam);
    db.dbDelete("file_num", sqlParam, res, next);
});
// 档案-编辑
router.post("/num/update", (req, res, next) => {
    let body = req.body;
    // console.log("update", req.body);
    let sqlParam = {
        // bid: body.bid,
        name: body.name,
        file_number: body.file_number,
        remark: body.remark,
        edit_time: uitl.getNewTime()
    };
    // console.log("update", sqlParam);
    db.dbUpdate("file_num", sqlParam, res, next, body.bid);
});
// 档案-通过id查询
router.post("/num/queryId", (req, res, next) => {
    let body = req.body;
    // console.log("queryById_data", req.body);
    let sqlParam = {
        id: body.id,
    };
    // console.log("queryById_data", sqlParam);
    db.dbQueryById("file_num", sqlParam, res, next);
});
// 档案-查询列表
router.get("/num/query", (req, res, next) => {
    db.queryList("file_num", req, res, next);
});

module.exports = router;